package com.tools.s7.mvp;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.tools.s7.base.CompatActivity;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;


public abstract class BaseMActivity<P extends BasePresenter> extends CompatActivity {

    protected P mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        mPresenter = GenericsUtils.getT(BaseMActivity.class);
//        mPresenter = GenericsUtils.getT(this, 0);
//        createPresenter();
        //判断下当前的activity是否实现了view的接口，如果实现了，把m，v给注入到p层
//        if (this instanceof BaseView){
//            if (getBundle() != null) {
//                mPresenter.attachView((BaseView)this, getBundle());
//            } else {
//                mPresenter.attachView((BaseView)this);
//            }
//        }

    }

    protected void createPresenter() {
        //这里获得到的是类的泛型的类型
        ParameterizedType type = (ParameterizedType) getClass().getGenericSuperclass();
        if (type != null) {
            Type[] actualTypeArguments = type.getActualTypeArguments();
            Class<P> tClass = (Class<P>) actualTypeArguments[0];
            mPresenter = PresenterFactory.creatPresenter(tClass);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.dettachView();
        }
    }
}
